
package harlequin;

import java.awt.Color;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class AttendanceDLSU extends GeneralAttendance{
    
    private String query = "insert into attendancedlsu(idNumber, fullName, emailAddress, courseSection, withIncentive, dateTimeslot"
                      + ") values(?,?,?,?, ?, ?)";
    private String addAudience = "Update play set audienceCount = ?, slot = ? where dateTimeslot = ?";
    private int incentive;
    private int audienceCount;
    private int slot;
    
    public AttendanceDLSU(Connection connector) {
        this.connector = connector;
        this.setLocationByPlatform(rootPaneCheckingEnabled);
        this.setTitle("Attendance");
   
        initComponents();
        initializeShowComboList2();
  
        
        
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cAttendance = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jC = new javax.swing.JLabel();
        cSection = new javax.swing.JComboBox();
        bSubmit = new javax.swing.JButton();
        tEmailAddress = new javax.swing.JTextField();
        tFullName = new javax.swing.JTextField();
        tIDNumber = new javax.swing.JTextField();
        bBack = new javax.swing.JButton();
        cShow = new javax.swing.JComboBox();
        cIncentive = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        cAttendance.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DLSU Student", "General Public" }));
        cAttendance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cAttendanceActionPerformed(evt);
            }
        });
        getContentPane().add(cAttendance, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 12, -1, -1));

        jLabel6.setText("ID Number:");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 69, -1, -1));

        jLabel7.setText("Full Name:");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 98, -1, -1));

        jLabel9.setText("E-mail Address:");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 127, -1, -1));

        jC.setForeground(new java.awt.Color(153, 153, 153));
        jC.setText("Course-Section:");
        getContentPane().add(jC, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 156, -1, -1));

        cSection.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cSectionActionPerformed(evt);
            }
        });
        getContentPane().add(cSection, new org.netbeans.lib.awtextra.AbsoluteConstraints(108, 156, 198, -1));

        bSubmit.setText("Submit");
        bSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSubmitActionPerformed(evt);
            }
        });
        getContentPane().add(bSubmit, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 250, 296, -1));
        getContentPane().add(tEmailAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(108, 124, 198, -1));
        getContentPane().add(tFullName, new org.netbeans.lib.awtextra.AbsoluteConstraints(108, 95, 198, -1));
        getContentPane().add(tIDNumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(108, 66, 198, -1));

        bBack.setText("Back");
        bBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBackActionPerformed(evt);
            }
        });
        getContentPane().add(bBack, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 282, 296, -1));

        cShow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cShowActionPerformed(evt);
            }
        });
        getContentPane().add(cShow, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 13, -1, -1));

        cIncentive.setText("With Incentive");
        cIncentive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cIncentiveActionPerformed(evt);
            }
        });
        getContentPane().add(cIncentive, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 38, -1, -1));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/HTGBG320320.jpg"))); // NOI18N
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 320, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void bSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSubmitActionPerformed

    try {
            fullName = tFullName.getText();
            idNumber = tIDNumber.getText();
            emailAddress = tEmailAddress.getText();
            
            if(emailAddress.isEmpty() && fullName.isEmpty() && idNumber.isEmpty())
               JOptionPane.showMessageDialog(null, "Please input all empty fields.");
              //jodelse if (cSection.getSelectedItem().equals(null))
                //JOptionPane.showMessageDialog(null, "Please Select your Section");
              else if (fullName.isEmpty())
                JOptionPane.showMessageDialog(null, "Please indicate your Full Name");
              else if (idNumber.isEmpty())
                JOptionPane.showMessageDialog(null, "Please indicate ID Number");
              else if (idNumber.length() < 8 || idNumber.length() > 8)
                JOptionPane.showMessageDialog(null, "Invalid ID Number");
              else {

            
            java.sql.PreparedStatement preparedStatement = connector.prepareStatement(query);
            preparedStatement.setString(1, idNumber);
            preparedStatement.setString(2, fullName);
            preparedStatement.setString(3, emailAddress);
            preparedStatement.setString(4, courseSection);
            preparedStatement.setInt(5, incentive);
            preparedStatement.setString(6, this.tempDate);
            

            preparedStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "Addition Success!");    
            addAttendance(this.tempDate);
            this.dispose();
            tIDNumber.setText("");
            tFullName.setText("Last/First/Middle");
            AttendanceDLSU attendance = new AttendanceDLSU(connector);
            attendance.setVisible(true);
              }
        } catch (SQLException ex) {
            Logger.getLogger(AttendanceDLSU.class.getName()).log(Level.SEVERE, null, ex);
        }

}//GEN-LAST:event_bSubmitActionPerformed

private void cAttendanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cAttendanceActionPerformed
   if(cAttendance.getSelectedItem() == "General Public")
   {
       this.dispose();
       new AttendancePublic(connector).setVisible(true);
   }
}//GEN-LAST:event_cAttendanceActionPerformed

private void cIncentiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cIncentiveActionPerformed

  if((cIncentive.isSelected())){
            
    jC.enable();
    jC.setForeground(Color.black);
    cSection.enable();
            cSection.removeAllItems();
                initializeSectionCombo(cShow.getSelectedItem().toString());
                incentive = 1;
                courseSection = cSection.getSelectedItem().toString();

                 }
  else if(!(cIncentive.isSelected())){
      jC.setForeground(Color.gray);
      jC.disable();
      cSection.disable();
      incentive = 0;
      courseSection = null;
      
      if(cSection.getItemCount() != 0)
          cSection.removeAllItems();
  }
}//GEN-LAST:event_cIncentiveActionPerformed

private void cShowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cShowActionPerformed
    tempDate = cShow.getSelectedItem().toString();
}//GEN-LAST:event_cShowActionPerformed

private void bBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBackActionPerformed
    this.dispose();
}//GEN-LAST:event_bBackActionPerformed

private void cSectionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cSectionActionPerformed
    if(incentive == 1)
        courseSection = cSection.getSelectedItem().toString();
    else
        courseSection = "";
}//GEN-LAST:event_cSectionActionPerformed



private void initializeShowComboList2(){
try {
  Statement stmt = connector.createStatement();
  ResultSet rs = stmt.executeQuery("Select * from play");
  while(rs.next()){
    cShow.addItem(rs.getString("dateTimeslot"));
  }
  } catch (SQLException ex) {
      Logger.getLogger(AttendanceDLSU.class.getName()).log(Level.SEVERE, null, ex);
  }   
}

private void addAttendance(String tempDate){
    
  try {
  Statement stmt = connector.createStatement();
  ResultSet rs = stmt.executeQuery("Select audienceCount, slot from play where dateTimeslot = \"" + tempDate + "\"");
  while(rs.next()){
    audienceCount = rs.getInt("audienceCount") + 1;
    slot = rs.getInt("slot") - 1;
    PreparedStatement preparedStatement = connector.prepareStatement(addAudience);
    preparedStatement.setInt(1, audienceCount);
    preparedStatement.setInt(2, slot);
    preparedStatement.setString(3, tempDate);
    preparedStatement.executeUpdate();
    
  }
  } catch (SQLException ex) {
      Logger.getLogger(AttendanceDLSU.class.getName()).log(Level.SEVERE, null, ex);
  }   
  
  System.out.println(audienceCount);
}


private void initializeSectionCombo(String date){
    
    try {
  Statement stmt = connector.createStatement();
  ResultSet rs = stmt.executeQuery("Select * from section where dateTimeslot = \"" + date + "\" ");
  while(rs.next()){
    cSection.addItem(rs.getString("courseSection"));
  }
  } catch (SQLException ex) {
      Logger.getLogger(HomePage.class.getName()).log(Level.SEVERE, null, ex);
  }   
}

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBack;
    private javax.swing.JButton bSubmit;
    private javax.swing.JComboBox cAttendance;
    private javax.swing.JCheckBox cIncentive;
    private javax.swing.JComboBox cSection;
    private javax.swing.JComboBox cShow;
    private javax.swing.JLabel jC;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField tEmailAddress;
    private javax.swing.JTextField tFullName;
    private javax.swing.JTextField tIDNumber;
    // End of variables declaration//GEN-END:variables
}
